/*
 * @描述:
 * @作者: 约訂
 * @Date: 2024-06-12 10:09:35
 * @LastEditTime: 2024-06-20 10:50:52
 * @修改人员: 约訂
 */
import { login, logout, getUserInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";
import CryptoJS from "../../utils/cryptoData";
const user = {
  state: {
    token:getToken(),
    userInfo: [],
    // 第一次加载菜单时用到
    loadMenus: false,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    SET_LOAD_MENUS: (state, loadMenus) => {
      state.loadMenus = loadMenus;
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(
          userInfo.username,
          userInfo.password,
          userInfo.code,
          userInfo.uuid
        )
          .then((res) => {
            if (res.statusCode === 200) {
              setToken(res.data);
              commit("SET_TOKEN", res.data);
              // 第一次加载菜单时用到， 具体见 src 目录下的 permission.js
              commit("SET_LOAD_MENUS", true);
            }
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    GetUserInfo({ commit }) {
      console.log(123);
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((res) => {
            setUserInfo(res.data, commit);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then((res) => {
            logOut(commit);
            resolve();
          })
          .catch((error) => {
            logOut(commit);
            reject(error);
          });
      });
    },

    updateLoadMenus({ commit }) {
      return new Promise((resolve, reject) => {
        commit("SET_LOAD_MENUS", false);
      });
    },
  },
};

export const logOut = (commit) => {
  commit("SET_TOKEN", "");
  commit("SET_USERINFO", []);
  removeToken();
};

export const setUserInfo = (res, commit) => {
  commit("SET_USERINFO", res);
};

export default user;
